/**
  * layui 扩展公共模块
  * Autor: Fhua
  * Date: 16-11-25
  */

layui.define(['layer'], function (exports) {
    var $ = layui.jquery;

    var obj = {
        isEmpty:function(val){
            return val==null || val=="" || $.trim(val) == "";
        },
        ajax: function (url, data, callback) {
            $.ajax({
                cache:false,
                url: url,
                type: 'post',
                dataType: 'json',
                data: data,
                success: function (data, startic) {
                    if (data.errno == 0) {
                        if(callback){
                            callback(data)
                        }else{
                            obj.layerAlertS(data.message, '提示');
                        }
                    } else {
                        obj.layerAlertE(data.message, '提示');
                    }
                },
                error: function () {
                    obj.layerAlertE('请求发生错误', '提示');
                }
            });
        },
        layerDel: function (title, text, url, data, callback) {
            parent.layer.confirm(text, {
                title: title,
                btnAlign: 'c',
                resize: false,
                icon: 3,
                btn: ['确定删除', '容我想想'],
                yes: function () {
                    obj.ajax(url, data, callback);
                }
            });
        },
        //成功提示
        layerAlertS: function (text, title) {
            parent.layer.msg(text,{icon:1});
            /*parent.layer.alert(text, {
                title: title,
                icon: 1,
                time: 5000,
                resize: false,
                zIndex: layer.zIndex,
                anim: Math.ceil(Math.random() * 6)
            });*/
        },
        //错误提示
        layerAlertE: function (text, title) {
            parent.layer.alert(text, {
                title: title,
                icon: 2,
                time: 5000,
                resize: false,
                zIndex: layer.zIndex,
                anim: Math.ceil(Math.random() * 6)
            });
        },
        //信息提示
        layerAlertI: function (text) {
            parent.layer.alert(text, {
                time: 5000,
                resize: false,
                zIndex: layer.zIndex,
                anim: Math.ceil(Math.random() * 6)
            });
            return;
        },


        layerPrompt: function () {},
        //询问层
        layerConfirm: function () {},
        //退出系统
        signOut: function (title, text, url, rturl, type, dataType, data, callback) {
            parent.layer.confirm(text, {
                title: title,
                resize: false,
                btn: ['确定退出', '容我想想'],
                btnAlign: 'c',
                icon: 3

            }, function () {

                location.href = rturl;
            }, function () {
                layer.msg('也可以这样！', {
                    time: 20000, //20s后自动关闭
                    btnAlign: 'c',
                    btn: ['明白了']
                });
            });
        }

    }

    exports("common", obj);
});